"""
__author__ = '霍格沃兹测试开发学社'
__desc__ = '更多测试开发技术探讨，请访问：https://ceshiren.com/t/topic/15860'
"""
from selenium.webdriver import ActionChains
from selenium.webdriver.common.by import By

from web_auto_testing.page.base_page import BasePage
from web_auto_testing.page.order_page import OrderPage


class HomePage(BasePage):
    """首页"""

    _BTN_MANAGE = (By.XPATH, "//*[text()='商场管理']")
    _BTN_CATEGORY = (By.XPATH, "//*[text()='商品类目']")
    _BTN_POPULARITY = (By.XPATH, "//a[text()='更多新品首发']")
    _BTN_HOT = (By.XPATH, "//a[text()='更多人气推荐']")
    _BTN_ADD = (By.CSS_SELECTOR, ".van-goods-action>button:nth-of-type(1)")
    _BTN_ADD2 = (By.CSS_SELECTOR, ".van-sku-actions>button:nth-of-type(1)")

    def place_an_order(self, title):
        """下单"""
        # 滚动到更多新品首发
        ActionChains(self.driver).scroll_to_element(self.do_find(self._BTN_HOT)).perform()
        # 点击更多新品首发
        self.do_click(self._BTN_POPULARITY)
        # 点击商品
        BTN_TESTOP = (By.XPATH, f"//div[text()='{title}']")
        ActionChains(self.driver).scroll_to_element(self.do_find(BTN_TESTOP)).perform()
        self.do_click(BTN_TESTOP)
        # 点击加入购物车
        self.do_click(self._BTN_ADD)
        self.do_click(self._BTN_ADD2)
        return self

    def go_to_cart(self):
        self.do_get("https://litemall.hogwarts.ceshiren.com/vue/index.html#/order")
        return OrderPage(self.driver)
